package com.yiqi.liebang.feature.mine.model;

import android.text.TextUtils;

import com.suozhang.framework.component.http.RxDataProcessFactory;
import com.yiqi.liebang.entity.bo.WorkBo;
import com.yiqi.liebang.feature.mine.contract.WorkContract;
import com.yiqi.liebang.feature.mine.dependencies.work.DaggerWorkModelComponent;
import com.yiqi.liebang.framework.api.ApiLib;
import com.yiqi.liebang.framework.api.UserApi;

import java.io.File;

import javax.inject.Inject;

import io.reactivex.Observable;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

public class WorkModel implements WorkContract.Model {

    @Inject
    UserApi api;


    @Inject
    public WorkModel() {
        DaggerWorkModelComponent.builder().apiComponent(ApiLib.apiComponent()).build().inject(this);
    }


    @Override
    public Observable<String> userAddWork(String comLogo, String company, String position,
                                          String beginTime, String endTime, String describeInfo) {
        MultipartBody.Part headFilePart = null;

        if (!TextUtils.isEmpty(comLogo)) {
            File headFile = new File(comLogo);
            RequestBody requestFile = RequestBody.create(MediaType.parse("application/otcet-stream"), headFile);
            headFilePart = MultipartBody.Part.createFormData("comLogo", headFile.getName(), requestFile);
        }

        RequestBody companyBody = RequestBody.create(MediaType.parse("multipart/form-data"), company);
        RequestBody positionBody = RequestBody.create(MediaType.parse("multipart/form-data"), position);
        RequestBody beginTimeBody = RequestBody.create(MediaType.parse("multipart/form-data"), beginTime);
        RequestBody endTimeBody = RequestBody.create(MediaType.parse("multipart/form-data"), endTime);
        RequestBody describeInfoBody = RequestBody.create(MediaType.parse("multipart/form-data"), describeInfo);
        return api.addWork(headFilePart, companyBody, positionBody, beginTimeBody, endTimeBody, describeInfoBody).compose(RxDataProcessFactory.dataPrepAndIoToMainTransformer());
    }

    @Override
    public Observable<String> userEditWork(String id, String comLogo, String company, String position, String beginTime, String endTime, String describeInfo) {
        MultipartBody.Part headFilePart = null;

        if (!TextUtils.isEmpty(comLogo)) {
            File headFile = new File(comLogo);
            RequestBody requestFile = RequestBody.create(MediaType.parse("application/otcet-stream"), headFile);
            headFilePart = MultipartBody.Part.createFormData("comLogo", headFile.getName(), requestFile);
        }
        RequestBody idBody = RequestBody.create(MediaType.parse("multipart/form-data"), id);
        RequestBody companyBody = RequestBody.create(MediaType.parse("multipart/form-data"), company);
        RequestBody positionBody = RequestBody.create(MediaType.parse("multipart/form-data"), position);
        RequestBody beginTimeBody = RequestBody.create(MediaType.parse("multipart/form-data"), beginTime);
        RequestBody endTimeBody = RequestBody.create(MediaType.parse("multipart/form-data"), endTime);
        RequestBody describeInfoBody = RequestBody.create(MediaType.parse("multipart/form-data"), describeInfo);
        return api.editWork(idBody,headFilePart, companyBody, positionBody, beginTimeBody, endTimeBody, describeInfoBody).compose(RxDataProcessFactory.dataPrepAndIoToMainTransformer());
    }

    @Override
    public Observable<WorkBo> userGetWork(WorkBo workBo) {
        return api.getWork(workBo).compose(RxDataProcessFactory.dataPrepAndIoToMainTransformer());
    }

    @Override
    public Observable<String> userDeleteWork(WorkBo workBo) {
        return api.deleteWork(workBo).compose(RxDataProcessFactory.dataPrepAndIoToMainTransformer());
    }
}
